package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SystemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhongchen
 * @date : 2020/2/13 14:02
 */
@Service(interfaceClass = SystemService.class)
@Transactional
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemDao systemDao;

    @Override
    public PageResult findPermissionPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> permissionByCondition = systemDao.findPermissionByCondition(queryString);
        return new PageResult(permissionByCondition.getTotal(), permissionByCondition.getResult());
    }

    @Override
    public void addPermission(Permission permission) {
        if (permission == null) {
            throw new RuntimeException();
        }
        systemDao.addPermission(permission);
    }

    @Override
    public void deletePermission(Integer id) {
        if (id <= 0) {
            throw new RuntimeException();
        }
        Integer count = systemDao.findRoleCountByPermissionId(id);
        if (count > 0) {
            throw new RuntimeException();
        }
        systemDao.deletePermission(id);
    }

    @Override
    public Permission findPermissionById(Integer id) {
        if (id <= 0) {
            throw new RuntimeException();
        }
        return systemDao.findPermissionById(id);
    }

    @Override
    public void editPermission(Permission permission) {
        if (permission == null) {
            throw new RuntimeException();
        }
        systemDao.editPermission(permission);
    }

    @Override
    public List<Permission> findPermissionAll() {
        return systemDao.findPermissionAll();
    }

    @Override
    public PageResult findRolePage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> roleByCondition = systemDao.findRoleByCondition(queryString);
        return new PageResult(roleByCondition.getTotal(), roleByCondition.getResult());
    }

    @Override
    public void addRole(Role role, Integer[] permissionIds) {
        systemDao.addRole(role);
        for (Integer permissionId : permissionIds) {
            Map map = new HashMap();
            map.put("role_id", role.getId());
            map.put("permission_id", permissionId);
            systemDao.bindRoleAndPermission(map);
        }
    }

    @Override
    public List<Integer> findRelationOnRoleAndPermission(Integer id) {
        return systemDao.findRelationOnRoleAndPermission(id);
    }

    @Override
    public Role findRoleById(Integer id) {
        return systemDao.findRoleById(id);
    }

    @Override
    public void editRole(Role role, Integer[] permissionIds) {
        systemDao.editRole(role);
        systemDao.removeRelationOnRoleAndPermission(role.getId());
        for (Integer permissionId : permissionIds) {
            Map map = new HashMap();
            map.put("role_id", role.getId());
            map.put("permission_id", permissionId);
            systemDao.bindRoleAndPermission(map);
        }
    }

    @Override
    public void deleteRole(Integer id) {
        Integer count = systemDao.findUserCountByRoleId(id);
        if (count > 0) {
            throw new RuntimeException();
        }
        systemDao.removeRelationOnRoleAndPermission(id);
        systemDao.deleteRole(id);
    }

    @Override
    public PageResult findUserPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<User> userByCondition = systemDao.findUserByCondition(queryString);
        return new PageResult(userByCondition.getTotal(), userByCondition.getResult());
    }

    @Override
    public List<Role> findRoleAll() {
        return systemDao.findRoleAll();
    }

    @Override
    public void addUser(User user, Integer[] roleIds) {
        systemDao.addUser(user);
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("user_id", user.getId());
            map.put("role_id", roleId);
            systemDao.bindUserAndRole(map);
        }
    }

    @Override
    public List<Integer> findRelationOnUserAndRole(Integer id) {
        return systemDao.findRelationOnUserAndRole(id);
    }

    @Override
    public User findUserById(Integer id) {
        return systemDao.findUserById(id);
    }

    @Override
    public void editUser(User user, Integer[] roleIds) {
        systemDao.editUser(user);
        systemDao.removeRelationOnUserAndRole(user.getId());
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("user_id", user.getId());
            map.put("role_id", roleId);
            systemDao.bindUserAndRole(map);
        }
    }

    @Override
    public void deleteUser(Integer id) {
        systemDao.removeRelationOnUserAndRole(id);
        systemDao.deleteUser(id);
    }
}
